import java.util.Arrays;
import java.util.PriorityQueue;

public class Solution1337 {
    static class Row{
        int line;
        int number;

        public Row(int line, int number) {
            this.line = line;
            this.number = number;
        }
    }

    public int[] kWeakestRows(int[][] mat, int k) {
        PriorityQueue<Row> queue=new PriorityQueue<>((a,b)->b.number==a.number?a.line-b.line:a.number-b.number);
        for (int i=0;i<mat.length;i++){
            int number=0;
            for (int j=0;j<mat[i].length;j++){
                if (mat[i][j]==1){
                    number++;
                }else{
                    break;
                }
            }
            queue.offer(new Row(i,number));
        }

        int[] ans=new int[k];
        for (int i=0;i<k;i++){
            ans[i]=queue.poll().line;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution1337().kWeakestRows(new int[][]{{1,0,0,0},{1,1,1,1},{1,0,0,0},{1,0,0,0}},2)));
    }
}
